//
//  YLYPieInfoViewController.m
//  iEasyBill
//
//  Created by 楊盧银Mac on 14-5-20.
//  Copyright (c) 2014年 com.yly16. All rights reserved.
//

#import "YLYPieInfoViewController.h"

@interface YLYPieInfoViewController ()

@end

@implementation YLYPieInfoViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.rootList = [self.rootDictionary objectForKey:@"list"];
    iBillType *type = [[DatabaseManager shareManager]showBillTypeByTypeID:[[self.rootDictionary objectForKey:@"type"] integerValue]];
    self.title =  type.typeName;
    
    self.rootList = [self showBillTypeSubByList:self.rootList];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return self.rootList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    
    return [[[self.rootList objectAtIndex:section] objectForKey:@"list"] count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    
    NSDictionary *dic = [self.rootList objectAtIndex:indexPath.section];
    NSMutableArray *array = [dic objectForKey:@"list"];
    iBill *theBill = [array objectAtIndex:indexPath.row];
    UIImageView *typeSubImageView = (UIImageView*)[cell viewWithTag:1];
    iBillTypeSub *sub = [[DatabaseManager shareManager]showBillSubTypeByTypeID:theBill.billTypeSubID.intValue];
    typeSubImageView.image = [UIImage imageWithData:sub.typeSubImageData];
    UILabel *subnamelabel = (UILabel*)[cell viewWithTag:12];
    subnamelabel.text = sub.typeSubName;
    
    UILabel *subMoneylabel = (UILabel*)[cell viewWithTag:13];
    subMoneylabel.text = [NSString stringWithFormat:@"￥%.2f",theBill.billMoney.floatValue];
    return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"%@\t\t合计：￥%.2f",[[self.rootList objectAtIndex:section] objectForKey:@"date"],[[[self.rootList objectAtIndex:section] objectForKey:@"money"] floatValue]];
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        NSDictionary *dic = [self.rootList objectAtIndex:indexPath.section];
        NSMutableArray *array = [dic objectForKey:@"list"];
        iBill *theBill = [array objectAtIndex:indexPath.row];
        [[DatabaseManager shareManager]removeABillIndex:theBill];
        [array removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */
-(NSMutableArray *)showBillTypeSubByList:(NSMutableArray*)list{
    NSMutableArray *iRootList = [[NSMutableArray alloc]init];
    for (iBill *aBill in list) {
        NSMutableDictionary *rootDic = [[NSMutableDictionary alloc]init];
        //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSString *destDateString = [dateFormatter stringFromDate:aBill.billTime];
        
        if (iRootList.count == 0) {
            NSMutableArray *array = [[NSMutableArray alloc]init];
            [rootDic setValue:destDateString forKey:@"date"];
            [array addObject:aBill];
            [rootDic setObject:array forKey:@"list"];
            [rootDic setObject:aBill.billMoney forKey:@"money"];
            [iRootList addObject:rootDic];
        }else{
            BOOL isOk = NO;
            for (NSMutableDictionary *dic in iRootList) {
                if ([[dic objectForKey:@"date"] isEqualToString:destDateString]) {
                    NSMutableArray *array = [dic objectForKey:@"list"];
                    [array addObject:aBill];
                    float sum = [[dic objectForKey:@"money"] floatValue];
                    sum += aBill.billMoney.floatValue;
                    [dic setObject:[NSNumber numberWithFloat:sum] forKey:@"money"];
                    isOk = YES;
                    break;
                }
            }
            if (isOk == NO) {
                NSMutableArray *array = [[NSMutableArray alloc]init];
                [rootDic setObject:destDateString forKey:@"date"];
                
                [rootDic setObject:aBill.billMoney forKey:@"money"];
                [array addObject:aBill];
                [rootDic setObject:array forKey:@"list"];
                
                [iRootList addObject:rootDic];
            }
        }
    }
    
    //排序
    int count = iRootList.count;
    for (int i = 0;  i < count-1; i++) {
        for (int k = 0; k < count -1; k++) {
            NSDictionary *currentdic = [iRootList objectAtIndex:k];
            NSDictionary *nextdic = [iRootList objectAtIndex:k+1];
            NSString *currentStr = [currentdic objectForKey:@"date"];
            NSString *nextStr = [nextdic objectForKey:@"date"];
            int a = [[[currentStr componentsSeparatedByString:@"-"] lastObject] integerValue];
            int b = [[[nextStr componentsSeparatedByString:@"-"] lastObject] integerValue];
            
            if (a < b) {
                [iRootList exchangeObjectAtIndex:k withObjectAtIndex:k+1];
            }
            
        }
    }
    
    return iRootList;
}
@end
